package com.hzhr.iot.web.controller;

import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.device.sdk.model.collectDevice.CollectDeviceModel;
import com.hzhr.iot.device.sdk.service.Device_CollectDeviceService;
import com.hzhr.iot.statis.sdk.model.deviceDailyGasConsume.DeviceDailyGasConsumeModel;
import com.hzhr.iot.statis.sdk.model.deviceMonthlyGasConsume.DeviceMonthlyGasConsumeModel;
import com.hzhr.iot.statis.sdk.service.Statis_DeviceDailyGasConsumeService;
import com.hzhr.iot.statis.sdk.service.Statis_DeviceMonthlyGasConsumeService;
import com.hzhr.iot.statis.sdk.service.Statis_GasUserDailyGasConsumeService;
import com.hzhr.iot.web.controller.base.BaseController;
import com.hzhr.iot.web.model.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;


@RequestMapping("/gasconsume")
@Api(description = "用气量")
@RestController
public class GasconsumeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    Statis_GasUserDailyGasConsumeService statis_gasUserDailyGasConsumeService;
    @Autowired
    Statis_DeviceDailyGasConsumeService statis_deviceDailyGasConsumeService;
    @Autowired
    Statis_DeviceMonthlyGasConsumeService statis_deviceMonthlyGasConsumeService;
    @Autowired
    Device_CollectDeviceService device_collectDeviceService;

    @ApiOperation(value = "燃气用户本周用气量", notes = "燃气用户本周用气量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gasuserId", value = "所属燃气用户编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "collectdeviceId", value = "采集设备编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping(value = "/week", method = RequestMethod.POST)
    BaseResponse week(
                       @RequestParam(value = "gasuserId", required = false,defaultValue = "0") String gasuserId,
                       @RequestParam(value = "collectdeviceId", required = false,defaultValue = "0") String collectdeviceId,
                       @RequestParam(value = "uat", required = true) String uat) throws Exception {
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);

        HashMap<String,Object> result=new HashMap<>();
        Map<String, Double> dataList = new TreeMap<>();
        Map<String, Double> collectdeviceList = new TreeMap<>();
        Double allcount = 0.0;

        //初始化dataList
        Long startTime = HzhrDateUtil.getBeginDayOfWeek().getTime();
        Long endTime = new Date().getTime();
        List<String> days =HzhrDateUtil.getDayBetween(HzhrDateUtil.dateToStr(new Date(startTime), "yyyy-MM-dd"),
                HzhrDateUtil.dateToStr(new Date(endTime), "yyyy-MM-dd"));
        for (String day :
                days) {
            dataList.put(day, 0.0);
        }

        if (!StringUtils.isEmpty(gasuserId) && !"0".equals(gasuserId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_gasUserId", gasuserId);
            query.put("show", "list");
            query.put("show_info", true);
            query.put("page", 1);
            query.put("count", 20);
            List<CollectDeviceModel> collectDeviceModels = device_collectDeviceService.get(header, query).getModelList();
            for (CollectDeviceModel collectDeviceModel :
            collectDeviceModels ) {
                Integer collectDeviceId = collectDeviceModel.getId();
                String collectDeviceCode = collectDeviceModel.getCode();

                Double collectDeviceSum = 0.0;

                query = new HashMap<>();
                query.put("equal_collectDeviceId", collectDeviceId);
                query.put("greaterthanorequal_date", startTime);
                query.put("lessthan_date", endTime);
                query.put("orderBy_date", "asc");
                query.put("show", "list");
                query.put("page", 1);
                query.put("count", 7);
                List<DeviceDailyGasConsumeModel> deviceDailyGasConsumeModels = statis_deviceDailyGasConsumeService.get(header, query).getModelList();
                for (DeviceDailyGasConsumeModel deviceDailyGasConsumeModel :
                        deviceDailyGasConsumeModels) {
                    String key = HzhrDateUtil.dateToStr(new Date(deviceDailyGasConsumeModel.getDate()), "yyyy-MM-dd");
                    Double value = deviceDailyGasConsumeModel.getData();
                    if (value != null && value > 0.0) {
                        for (Map.Entry<String, Double> entry :
                        dataList.entrySet()) {
                            String entryKey = entry.getKey();
                            if (entryKey.equals(key)) {
                                Double entryValue = entry.getValue();
                                entryValue += value;
                                dataList.put(entryKey, entryValue);

                                allcount += value;
                                collectDeviceSum += value;
                            }
                        }
                    }
                }

                collectdeviceList.put(collectDeviceCode, collectDeviceSum);
            }


        } else if (!StringUtils.isEmpty(collectdeviceId) && !"0".equals(collectdeviceId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_id", collectdeviceId);
            query.put("show", "one");
            query.put("show_info", true);
            CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();
            String collectDeviceCode = collectDeviceModel.getCode();

            Double collectDeviceSum = 0.0;

            query = new HashMap<>();
            query.put("equal_collectDeviceId", collectdeviceId);
            query.put("greaterthanorequal_date", HzhrDateUtil.getBeginDayOfWeek().getTime());
            query.put("lessthan_date", new Date().getTime());
            query.put("orderBy_date", "asc");
            query.put("show", "list");
            query.put("page", 1);
            query.put("count", 7);
            List<DeviceDailyGasConsumeModel> deviceDailyGasConsumeModels = statis_deviceDailyGasConsumeService.get(header, query).getModelList();
            for (DeviceDailyGasConsumeModel deviceDailyGasConsumeModel :
                    deviceDailyGasConsumeModels) {
                String key = HzhrDateUtil.dateToStr(new Date(deviceDailyGasConsumeModel.getDate()), "yyyy-MM-dd");
                Double value = deviceDailyGasConsumeModel.getData();
                if (value != null && value > 0.0) {
                    for (Map.Entry<String, Double> entry :
                            dataList.entrySet()) {
                        String entryKey = entry.getKey();
                        if (entryKey.equals(key)) {
                            Double entryValue = entry.getValue();
                            entryValue += value;
                            dataList.put(entryKey, entryValue);

                            allcount += value;
                            collectDeviceSum += value;
                        }
                    }
                }
            }

            collectdeviceList.put(collectDeviceCode, collectDeviceSum);
        }
        result.put("dataList", dataList);
        result.put("collectdeviceList",collectdeviceList);
        result.put("allcount",allcount);
        return new BaseResponse("0", "成功", result);
    }

    @ApiOperation(value = "本月用气量", notes = "本月用气量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gasuserId", value = "所属燃气用户编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "collectdeviceId", value = "采集设备编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping(value = "/month", method = RequestMethod.POST)
    BaseResponse month(
            @RequestParam(value = "gasuserId", required = false,defaultValue = "0") String gasuserId,
            @RequestParam(value = "collectdeviceId", required = false,defaultValue = "0") String collectdeviceId,
            @RequestParam(value = "uat", required = true) String uat) throws Exception {
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);

        HashMap<String,Object> result=new HashMap<>();
        Map<String, Double> dataList = new TreeMap<>();
        Map<String, Double> collectdeviceList = new TreeMap<>();
        Double allcount = 0.0;

        //初始化dataList
        Long startTime = HzhrDateUtil.getBeginDayOfMonth().getTime();
        Long endTime = new Date().getTime();
        List<String> days =HzhrDateUtil.getDayBetween(HzhrDateUtil.dateToStr(new Date(startTime), "yyyy-MM-dd"),
                HzhrDateUtil.dateToStr(new Date(endTime), "yyyy-MM-dd"));
        for (String day :
                days) {
            dataList.put(day, 0.0);
        }

        if (!StringUtils.isEmpty(gasuserId) && !"0".equals(gasuserId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_gasUserId", gasuserId);
            query.put("show", "list");
            query.put("show_info", true);
            query.put("page", 1);
            query.put("count", 20);
            List<CollectDeviceModel> collectDeviceModels = device_collectDeviceService.get(header, query).getModelList();
            for (CollectDeviceModel collectDeviceModel :
                    collectDeviceModels ) {
                Integer collectDeviceId = collectDeviceModel.getId();
                String collectDeviceCode = collectDeviceModel.getCode();

                Double collectDeviceSum = 0.0;

                query = new HashMap<>();
                query.put("equal_collectDeviceId", collectDeviceId);
                query.put("greaterthanorequal_date", startTime);
                query.put("lessthan_date", endTime);
                query.put("orderBy_date", "asc");
                query.put("show", "list");
                query.put("page", 1);
                query.put("count", 31);
                List<DeviceDailyGasConsumeModel> deviceDailyGasConsumeModels = statis_deviceDailyGasConsumeService.get(header, query).getModelList();
                for (DeviceDailyGasConsumeModel deviceDailyGasConsumeModel :
                        deviceDailyGasConsumeModels) {
                    String key = HzhrDateUtil.dateToStr(new Date(deviceDailyGasConsumeModel.getDate()), "yyyy-MM-dd");
                    Double value = deviceDailyGasConsumeModel.getData();
                    if (value != null && value > 0.0) {
                        for (Map.Entry<String, Double> entry :
                                dataList.entrySet()) {
                            String entryKey = entry.getKey();
                            if (entryKey.equals(key)) {
                                Double entryValue = entry.getValue();
                                entryValue += value;
                                dataList.put(entryKey, entryValue);

                                allcount += value;
                                collectDeviceSum += value;
                            }
                        }
                    }
                }

                collectdeviceList.put(collectDeviceCode, collectDeviceSum);
            }


        } else if (!StringUtils.isEmpty(collectdeviceId) && !"0".equals(collectdeviceId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_id", collectdeviceId);
            query.put("show", "one");
            query.put("show_info", true);
            CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();
            String collectDeviceCode = collectDeviceModel.getCode();

            Double collectDeviceSum = 0.0;

            query = new HashMap<>();
            query.put("equal_collectDeviceId", collectdeviceId);
            query.put("greaterthanorequal_date", HzhrDateUtil.getBeginDayOfMonth().getTime());
            query.put("lessthan_date", new Date().getTime());
            query.put("orderBy_date", "asc");
            query.put("page", 1);
            query.put("count", 31);
            query.put("show", "list");
            List<DeviceDailyGasConsumeModel> deviceDailyGasConsumeModels = statis_deviceDailyGasConsumeService.get(header, query).getModelList();
            for (DeviceDailyGasConsumeModel deviceDailyGasConsumeModel :
                    deviceDailyGasConsumeModels) {
                String key = HzhrDateUtil.dateToStr(new Date(deviceDailyGasConsumeModel.getDate()), "yyyy-MM-dd");
                Double value = deviceDailyGasConsumeModel.getData();
                if (value != null && value > 0.0) {
                    for (Map.Entry<String, Double> entry :
                            dataList.entrySet()) {
                        String entryKey = entry.getKey();
                        if (entryKey.equals(key)) {
                            Double entryValue = entry.getValue();
                            entryValue += value;
                            dataList.put(entryKey, entryValue);

                            allcount += value;
                            collectDeviceSum += value;
                        }
                    }
                }
            }

            collectdeviceList.put(collectDeviceCode, collectDeviceSum);
        }
        result.put("dataList", dataList);
        result.put("collectdeviceList",collectdeviceList);
        result.put("allcount",allcount);
        return new BaseResponse("0", "成功", result);
    }

    @ApiOperation(value = "本年用气量", notes = "本年用气量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gasuserId", value = "所属燃气用户编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "collectdeviceId", value = "采集设备编号", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "uat", value = "用户登录令牌", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping(value = "/year", method = RequestMethod.POST)
    BaseResponse year(
            @RequestParam(value = "gasuserId", required = false,defaultValue = "0") String gasuserId,
            @RequestParam(value = "collectdeviceId", required = false,defaultValue = "0") String collectdeviceId,
            @RequestParam(value = "uat", required = true) String uat) throws Exception {
        String token = getToken();
        Map<String, String> header = new HashMap<>();
        header.put("token", token);

        HashMap<String,Object> result=new HashMap<>();
        Map<String, Double> dataList = new TreeMap<>();
        Map<String, Double> collectdeviceList = new TreeMap<>();
        Double allcount = 0.0;

        //初始化dataList
        Long startTime = HzhrDateUtil.getBeginDayOfYear().getTime();
        Long endTime = new Date().getTime();
        List<String> months =HzhrDateUtil.getMonthBetween(HzhrDateUtil.dateToStr(new Date(startTime), "yyyy-MM"),
                HzhrDateUtil.dateToStr(new Date(endTime), "yyyy-MM"));
        for (String month :
                months) {
            dataList.put(month, 0.0);
        }

        if (!StringUtils.isEmpty(gasuserId) && !"0".equals(gasuserId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_gasUserId", gasuserId);
            query.put("show", "list");
            query.put("show_info", true);
            query.put("page", 1);
            query.put("count", 20);
            List<CollectDeviceModel> collectDeviceModels = device_collectDeviceService.get(header, query).getModelList();
            for (CollectDeviceModel collectDeviceModel :
                    collectDeviceModels ) {
                Integer collectDeviceId = collectDeviceModel.getId();
                String collectDeviceCode = collectDeviceModel.getCode();

                Double collectDeviceSum = 0.0;

                query = new HashMap<>();
                query.put("equal_collectDeviceId", collectDeviceId);
                query.put("greaterthanorequal_date", startTime);
                query.put("lessthan_date", endTime);
                query.put("orderBy_date", "asc");
                query.put("show", "list");
                query.put("page", 1);
                query.put("count", 12);
                List<DeviceMonthlyGasConsumeModel> deviceMonthlyGasConsumeModels = statis_deviceMonthlyGasConsumeService.get(header, query).getModelList();
                for (DeviceMonthlyGasConsumeModel deviceMonthlyGasConsumeModel :
                        deviceMonthlyGasConsumeModels) {
                    String key = HzhrDateUtil.dateToStr(new Date(deviceMonthlyGasConsumeModel.getDate()), "yyyy-MM");
                    Double value = deviceMonthlyGasConsumeModel.getData();
                    if (value != null && value > 0.0) {
                        for (Map.Entry<String, Double> entry :
                                dataList.entrySet()) {
                            String entryKey = entry.getKey();
                            if (entryKey.equals(key)) {
                                Double entryValue = entry.getValue();
                                entryValue += value;
                                dataList.put(entryKey, entryValue);

                                allcount += value;
                                collectDeviceSum += value;
                            }
                        }
                    }
                }

                collectdeviceList.put(collectDeviceCode, collectDeviceSum);
            }


        } else if (!StringUtils.isEmpty(collectdeviceId) && !"0".equals(collectdeviceId)) {
            Map<String, Object> query = new HashMap<>();
            query.put("equal_id", collectdeviceId);
            query.put("show", "one");
            query.put("show_info", true);
            CollectDeviceModel collectDeviceModel = device_collectDeviceService.get(header, query).getModel();
            String collectDeviceCode = collectDeviceModel.getCode();

            Double collectDeviceSum = 0.0;

            query = new HashMap<>();
            query.put("equal_collectDeviceId", collectdeviceId);
            query.put("greaterthanorequal_date", HzhrDateUtil.getBeginDayOfYear().getTime());
            query.put("lessthan_date", new Date().getTime());
            query.put("orderBy_date", "asc");
            query.put("show", "list");
            query.put("page", 1);
            query.put("count", 12);
            List<DeviceMonthlyGasConsumeModel> deviceMonthlyGasConsumeModels = statis_deviceMonthlyGasConsumeService.get(header, query).getModelList();
            for (DeviceMonthlyGasConsumeModel deviceMonthlyGasConsumeModel :
                    deviceMonthlyGasConsumeModels) {
                String key = HzhrDateUtil.dateToStr(new Date(deviceMonthlyGasConsumeModel.getDate()), "yyyy-MM");
                Double value = deviceMonthlyGasConsumeModel.getData();
                if (value != null && value > 0.0) {
                    for (Map.Entry<String, Double> entry :
                            dataList.entrySet()) {
                        String entryKey = entry.getKey();
                        if (entryKey.equals(key)) {
                            Double entryValue = entry.getValue();
                            entryValue += value;
                            dataList.put(entryKey, entryValue);

                            allcount += value;
                            collectDeviceSum += value;
                        }
                    }
                }
            }

            collectdeviceList.put(collectDeviceCode, collectDeviceSum);
        }
        result.put("dataList", dataList);
        result.put("collectdeviceList",collectdeviceList);
        result.put("allcount",allcount);
        return new BaseResponse("0", "成功", result);
    }
}
